package org.osll.tictactoe.transport.rmi.client;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.osll.tictactoe.Control;
import org.osll.tictactoe.ServerConnection;
import org.osll.tictactoe.Team;
import org.osll.tictactoe.transport.DefaultOptions;

public class ServerConnectionImpl implements ServerConnection {
	
	org.osll.tictactoe.transport.rmi.ServerConnection service = null;
	
	public ServerConnectionImpl() {
		try {
			Registry registry = LocateRegistry.getRegistry(DefaultOptions.getInstance().getHost());
			service = (org.osll.tictactoe.transport.rmi.ServerConnection)registry.lookup("TicTacToe_LoginServer");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public Control connect(String name, Team team) {
		Control c = null;
		try {
			c = new ControlImpl(service.connect(name, team),name,team);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}

}
